import {
    ADD_GOODS,
    INIT_SHOP_CART,
    REDUCE_CART,
    SELECTED_SINGER_GOODS,
    SELECTED_ALL,
    CLEAR_CART,
    USER_INFO,
    INIT_USER_INFO,
    RESET_USER_INFO

} 
from './mutations-type'
import {getStore,setStore, removeStore} from './../config/global';
import Vue from 'vue'

export default{
    [ADD_GOODS](state,{goodsId,goodsName,smallImage,goodsPrice}){
        let shopCart = state.shopCart;
        if (shopCart[goodsId]) {
            shopCart[goodsId]['num']++;
        }else{
            shopCart[goodsId] = {
                'num':1,
                'id':goodsId,
                'name':goodsName,
                'small_image':smallImage,
                'price':goodsPrice,
                'checked':true
              }
        }
        state.shopCart = {...shopCart};
        // 存入本地
        setStore('shopCart',state.shopCart);
    },
    // 页面初始化,获取购物车的数据(本地)
    [INIT_SHOP_CART](state){
        let initCart = getStore('shopCart');
        if (initCart) {
            state.shopCart = JSON.parse(initCart);
        }
    },
    // 移除购物车
    [REDUCE_CART](state,{goodsId}){
        let shopCart = state.shopCart;
        let goods = shopCart[goodsId];
        if (goods) {
            if (goods['num'] > 0) {
                goods['num']--;
                if (goods['num'] === 0) {
                    delete shopCart[goodsId];
                }
               
            }else{
                goods = null;
            }
             // 同步数据
            state.shopCart = {...shopCart};
            setStore('shopCart',state.shopCart);
        }
    },
    // 单个商品取消,使用
    [SELECTED_SINGER_GOODS](state,{goodsId}){
        let shopCart = state.shopCart;
        let goods = shopCart[goodsId];
        if (goods) {
            if (goods.checked) {
                goods.checked = !goods.checked
            }else{
                Vue.set(goods,'checked',true)
            }
            state.shopCart = {...shopCart};
            setStore('shopCart',state.shopCart);
        }
    },
    // 全选/取消全选
    [SELECTED_ALL](state,{isSelected}){
        let shopCart = state.shopCart;
        Object.values(shopCart).forEach((goods,index)=>{
            if (goods.checked) {
                goods.checked = !isSelected
            }else{
                Vue.set(goods,'checked',!isSelected)
            }
        });
        state.shopCart = {...shopCart};
        setStore('shopCart',state.shopCart);
    },
    [CLEAR_CART](state){
        state.shopCart = null;
        state.shopCart = {...state.shopCart};
        setStore('shopCart',state.shopCart);
    },
    // 7 保存用户信息到本地
    [USER_INFO](state,{userInfo}){
        state.userInfo = userInfo;
        setStore('userInfo',state.userInfo);
    },
    // 7 保存用户信息到本地
    [INIT_USER_INFO](state){
        let userInfo = getStore('userInfo');
        if(userInfo){
            state.userInfo = JSON.parse(userInfo);
        }
    },
    [RESET_USER_INFO](state){
        state.userInfo = {};
        removeStore;
    }
}
 